/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright (c) 2008-2009, The KiWi Project (http://www.kiwi-project.eu)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, 
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 * - Neither the name of the KiWi Project nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Contributor(s):
 *   Thomas Kurz
 * 
 */

/**
 * 
 * 
 * I updated to be compatible with jQuery.ui 1.8 /Szaby/
 */
(function($){
	$.widget("kiwi.status", {
		/**
		 * Constructor; 
		 */
		_init: function() { 
			this.refresh();
		},
		
		refresh: function() {
            var webservice = this.getWebServiceUrl();
            var frequence = this.getFrequence();
			var img1 = this.getImageStopped();
			var img2 = this.getImageRunning();
			
			var tasks = null;
			
			function hoverStatusDiv() {
				if( tasks != null ) {
					var ttext="<ul style='padding-left:20px'>";
					for( var i = 0; i < tasks.length; i++ ) {
						ttext+="<li><b>"+tasks[i].status.@name+"</b>";

                                                if (tasks[i].status.progress > -1)
                                                    ttext+=" ("+tasks[i].status.@progress+"&#x25;)";

                                                ttext+="<br/>"+tasks[i].status.@message+"</li>";
					}
					ttext+="</ul>";
					jQuery("#statusImageMessage").html(ttext);
					jQuery("#statusImageMessage").show();
				} else {
					jQuery("#statusImageMessage").html("<ul style='padding-left:20px'><li>0 tasks in schedule</li></ul>");
					jQuery("#statusImageMessage").show();
				}
			}
			
            var element = this.element;
			var messageDiv = "<div id='statusImageMessage' style='display:none;position:absolute;top:80px;left:100%;width:200px;margin-left:-202px;background-color:#F5F6CE;border:1px solid black;' />";
			element.html("<img id='statusImage' src='"+img1+"' />"+messageDiv);
			
			jQuery("#statusImage").hover(hoverStatusDiv);
			jQuery("#statusImageMessage").hover(function(){},function(){
				jQuery("#statusImageMessage").fadeOut();
			});
			
			function getStatus(){
			
			
				// call the RecommendationsWidgetWebService using JSONP and create a <ul> list of items
				$.getJSON(webservice + "/status?jsonpCallback=?", function(data){
				
					var html = "";
					
					if (data.length == 0) {
						jQuery("#statusImage").attr('src',img1);
						tasks = null;			
					}
					else {
						jQuery("#statusImage").attr('src',img2);
						tasks = data;
					}
					setTimeout(function(){
						getStatus();
					}, frequence);
					
				});
			}
			
			getStatus();
						
		},
		
		/**
		 * The URL of the web service to connect to
		 */
		getWebServiceUrl: function() {
			return this._getData?this._getData("webServiceUrl"):this.option()["webServiceUrl"];
		},
		
		getFrequence: function() {
			return this._getData?this._getData("frequence"):this.option()["frequence"];
		},
		
		getImageStopped: function() {
			return this._getData?this._getData("img_stopped"):this.option()["img_stopped"];
		},
		
		getImageRunning: function() {
			return this._getData?this._getData("img_running"):this.option()["img_running"];
		}
	});
	
	$.kiwi.status.defaults = {
		webServiceUrl: 'http://localhost:8080/KiWi/seam/resource/services/widgets/status',
		frequence: 5000,
		img_running: '/img/running.gif',
		img_stopped: 'img/stopped.gif'	
	};
	$.kiwi.status.prototype.options = $.kiwi.status.defaults;
})(jQuery);
